﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Reflection.Emit;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;
using FlickrSample.SL.Infrastructure.Flickr.Entities;

namespace FlickrSample.SL.Infrastructure.Flickr.Parser
{
    public class PhotoParser
    {
        public static List<FlickrPhotoInstance> ParseInstances(FlickrPhoto photo, string xml)
        {
            if (!Common.CheckError(xml))
            {
                var doc = XDocument.Parse(xml);

                var list = new List<FlickrPhotoInstance>();

                foreach (var size in doc.Root.Element("sizes").Elements("size"))
                {
                    var newPhotoInstance = new FlickrPhotoInstance()
                                               {
                                                   Label = size.Attribute("label").Value,
                                                   Width = Convert.ToDouble(size.Attribute("width").Value),
                                                   Height = Convert.ToDouble(size.Attribute("height").Value),
                                                   Source = size.Attribute("source").Value,
                                                   FlickrUrl = size.Attribute("url").Value,




                                               };

                    list.Add(newPhotoInstance);

                    if (size.Attribute("media").Value != "photo")
                    {
                        photo.MediaType = FlickrMediaType.Video;
                    }
                    else
                    {
                        photo.MediaType = FlickrMediaType.Photo;
                    }
                }
                return list;
            }
            return null;
        }


        public static List<FlickrPhoto> ParsePhoto(string xml)
        {
            if (!Common.CheckError(xml))
            {
                var doc = XDocument.Parse(xml);

                var list = (from photo in doc.Root.Element("photoset").Elements("photo")
                            select new FlickrPhoto()
                                       {
                                           PhotoId = photo.Attribute("id").Value,
                                           IsPrimary = photo.Attribute("isprimary").Value == "1",
                                           Title = photo.Attribute("title").Value
                                       }).ToList();
                return list;
                //var list = new List<FlickrPhoto>();
                //http://farm5.static.flickr.com/4045/4585640079_2779363b29_t.jpg


                //foreach (var node in doc.Root.Element("photoset").Elements("photo"))
                //{
                //    var photo = new FlickrPhoto()
                //                    {

                //                    }
                //}
                /*
                 * <?xml version="1.0" encoding="utf-8" ?>
<rsp stat="ok">
<photoset id="72157623639219971" primary="4488228490" owner="24891669@N05" ownername="jakkaj10" page="1" per_page="500" perpage="500" pages="1" total="1">
    <photo id="4488228490" secret="8d68327d5c" server="4019" farm="5" title="Easter 2010" isprimary="0" />
</photoset>
</rsp>*/

            }

            return null;
        }
    }
}
